package com.campus.dao.impl;

import com.campus.dao.TeacherDao;
import com.campus.model.Teacher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author HLQ
 * @date 2024/11/17 22:02
 */
@Slf4j
@Repository
public class TeacherDaoImpl implements TeacherDao {
    @Autowired
    JdbcTemplate jdbcTemplate;
    private final RowMapper<Teacher> teacherRowMapper = new RowMapper<Teacher>() {
        @Override
        public Teacher mapRow(ResultSet rs,int rowNum)throws SQLException{
            Teacher teacher = new Teacher()
                    .setId(rs.getInt("id"))
                    .setTeacherName(rs.getString("teacher_name"))
                    .setSubject(rs.getString("subject"))
                    .setProfession(rs.getString("profession"))
                    .setSchool(rs.getString("school"))
                    .setIntroduce(rs.getString("introduce"))
                    .setLoginId(rs.getInt("login_id"));
            return teacher;
        }
    };
    @Override
    public int addTeacher(Teacher teacher) {
        String sql = "INSERT INTO t_manager_teacher " +
                "(teacher_name, subject, profession, school, introduce, login_id) " +
                "VALUES (?,?,?,?,?,?)";
        Object[] args = new Object[]{
                teacher.getTeacherName(),
                teacher.getSubject(),
                teacher.getProfession(),
                teacher.getSchool(),
                teacher.getIntroduce(),
                teacher.getLoginId()
        };
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public int deleteTeacherById(int id) {
        String sql = "DELETE FROM t_manager_teacher WHERE id = ?";
        return jdbcTemplate.update(sql,id);
    }

    @Override
    public int updateTeacherById(Teacher teacher) {
        String sql = "UPDATE t_manager_teacher SET " +
                "teacher_name = ?, subject = ?, profession = ?, school = ?, introduce = ?, login_id = ? WHERE id = ?";
        Object[] args = new Object[]{
                teacher.getTeacherName(),
                teacher.getSubject(),
                teacher.getProfession(),
                teacher.getSchool(),
                teacher.getIntroduce(),
                teacher.getLoginId(),
                teacher.getId()
        };
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public Teacher queryTeacherById(int id) {
        String sql = "SELECT * FROM t_manager_teacher WHERE id = ?";
        return jdbcTemplate.queryForObject(sql,new Object[]{id},teacherRowMapper);
    }

    @Override
    public Teacher queryTeacherByName(String name) {
        String sql = "SELECT * FROM t_manager_teacher WHERE teacher_name = ?";
        return jdbcTemplate.queryForObject(sql,new Object[]{name},teacherRowMapper);
    }
    @Override
    public int addClassStudentGroup(int classId,int studentId, int groupId) {
        String sql = "INSERT INTO t_teaching_student_in_class " +
                "(class_id, student_id, group_id) " + "VALUES (?,?,?)";
        Object[] args = new Object[]{
                classId,
                studentId,
                groupId
        };
        return jdbcTemplate.update(sql, args);
    }
}
